package org.mkh.cmd;



public class TestCommand extends AbstractCommand {
	
	//when method execute is called the field size will have the value of the field called size in the 
	//current instance of "org.mkh.cmd.DataModel" held in the Registry.
	@FieldInjectionMarker(name = "width", sourceClass="org.mkh.cmd.DataModel", returnValue = true)
	private int width;
	
	@FieldInjectionMarker(name = "height", sourceClass="org.mkh.cmd.DataModel", returnValue = false)
	private int height;
	
	@FieldInjectionMarker(name = "area", sourceClass="org.mkh.cmd.DataModel", returnValue = false)
	private int area;
	



    @Override
	@PreCommandCallbackMarker(targetClass="org.mkh.cmd.DataModel", methodName="pre" )
	@PostCommandCallbackMarker(targetClass="org.mkh.cmd.TestCommand", methodName="post" )
	public void execute() {
		
		area = width*height;
		
		System.out.println("width*height="+area);
		System.out.println("width="+width);
		
		width++;

	}

    public void post(){
        System.out.println("in post");
    }

	public static void main(String[] args){
		
		//make a model to play with
		DataModel model = new DataModel();
		//register it
		Registry.put(model);
		
		//make a command that uses data held in the DataModel
        TestCommand tc =  new TestCommand() ;
       Registry.put(tc);
        CommandIF tCmd = CommandFactory.createCommand(tc);
		Registry.put(tCmd);
		
		
		tCmd.execute();


        //uncomment to see "width" being put back into DataModel
        tCmd.execute();
        tCmd.execute();
        tCmd.execute();
        tCmd.execute();
        tCmd.execute();
        tCmd.execute();

		
		System.out.println("============ done ===========");
		
	}
}
